package com.myproject.source;

import java.io.IOException;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.AndroidHttpTransport;
import org.xmlpull.v1.XmlPullParserException;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;

public class Login extends Activity {
	private static final String SOAP_ACTION = "http://service.kacanbalikbuyukolur.com/test/CheckUser";
	private static final String METHOD_NAME = "CheckUser";
	private static final String NAMESPACE = "http://service.kacanbalikbuyukolur.com/test/";
	private static final String URL = "http://service.kacanbalikbuyukolur.com/test/getdeals.asmx";
	public static final String PREFS_NAME = "MyPrefsFile";
	private static final String PREF_USERNAME = "username";
	private static final String PREF_PASSWORD = "password";
	private static final String PREF_USERID = "userid";
	private static final String PREF_DEALSLAYOUT = "dealslayout";
	EditText username;
	EditText password;
	TextView error ;
	CheckBox rememberMe;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
		setContentView(R.layout.login);
		getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,
				R.layout.title_bar);
		SharedPreferences pref = getSharedPreferences(PREFS_NAME,MODE_PRIVATE);   
		String user = pref.getString(PREF_USERNAME, null);
		String pass = pref.getString(PREF_PASSWORD, null);
		String dealslayout = pref.getString(PREF_DEALSLAYOUT, null);
		username= (EditText) findViewById(R.id.Username);
		password = (EditText) findViewById(R.id.Password);
		error = (TextView) findViewById(R.id.tv_error);
		rememberMe = (CheckBox) findViewById(R.id.RememberMe);
		if (user == null || pass == null) {
		    //Prompt for username and password
			
		}
		else
		{
			rememberMe.setChecked(true);
			username.setText(user);
			password.setText(pass);
		}
		if (dealslayout == null ) {
			dealslayout="1";
			getSharedPreferences(PREFS_NAME,MODE_PRIVATE)
	        .edit()
	        .putString(PREF_DEALSLAYOUT, dealslayout)
	        .commit();
		}
		
	}

	public void LoginThing(View v) {
		

		// final Button btn_goster = (Button) findViewById(R.id.btn_login);
//		username= (EditText) findViewById(R.id.Username);
//		password = (EditText) findViewById(R.id.Password);
//		error = (TextView) findViewById(R.id.tv_error);
		SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
		request.addProperty("user", String.valueOf(username.getText()));
		request.addProperty("pass", String.valueOf(password.getText()));
		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
				SoapEnvelope.VER11);
		envelope.dotNet = true;
		envelope.setOutputSoapObject(request);

		AndroidHttpTransport httpTransport = new AndroidHttpTransport(URL);

		SoapPrimitive result = null;
		try {
			httpTransport.call(SOAP_ACTION, envelope);
			result = (SoapPrimitive) envelope.getResponse();

		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (XmlPullParserException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if (result.toString().equals("Error") ) {
			error.setText(result.toString());

		} else {
			if (rememberMe.isChecked())
			{
			getSharedPreferences(PREFS_NAME,MODE_PRIVATE)
	        .edit()
	        .putString(PREF_USERNAME, String.valueOf(username.getText()))
	        .putString(PREF_PASSWORD, String.valueOf(password.getText()))
	        .commit();
			}
			else
			{
				getSharedPreferences(PREFS_NAME,MODE_PRIVATE)
		        .edit()
		        .putString(PREF_USERNAME, null)
		        .putString(PREF_PASSWORD, null)
		        .commit();
			}
			getSharedPreferences(PREFS_NAME,MODE_PRIVATE)
	        .edit()
	        .putString(PREF_USERID, String.valueOf(result.toString()))
	        .commit();
			String KEY_USERID = "userid";
						Intent in = new Intent(getApplicationContext(),
					LoadingActivity.class);
						String id=result.toString();
			in.putExtra(KEY_USERID, id);
			
			startActivity(in);
//			startActivity(new Intent(Login.this,
//					LoadingActivity.class));
		}

	}
}
